package loadDataFromFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.json.JSONObject;

import com.fasterxml.jackson.databind.ObjectMapper;

import dao.impl.BookDaoImpl;
import pojo.Book;
import util.ActiveMqUtil;

public class LoadDataFromFile {
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException, JMSException {
		BookDaoImpl impl = new BookDaoImpl();
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.txt"));
		Destination queue = new ActiveMQQueue("matthew");
		JSONObject json = null;
		
		List<Book> list = (List<Book>) in.readObject();	
		
		Connection con = ActiveMqUtil.createConnection();
		con.start();
		Session sen = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
		MessageProducer producer = sen.createProducer(queue);
		System.out.println(list.size());
		for(Book book:list){
			ObjectMapper mapper = new ObjectMapper();
			String jsonString = mapper.writeValueAsString(book);
			TextMessage msg = sen.createTextMessage(jsonString.toString());
			producer.send(msg);
		}
		ActiveMqUtil.closeProducer(con, sen, producer);
	}
}
